#include <iostream>
#include <memory.h>
#include <stack>
#include <vector>
#include <cstdio>

#define MAXN 5005
using namespace std;

int dfn[MAXN], low[MAXN], color[MAXN], col[MAXN], sign[MAXN];
int sum = 0;
int n, m;
stack<int> s;
vector<int> G[MAXN];
int deep;

void tarjan (int u) {
	deep++;
	dfn[u] = deep;
	low[u] = deep;
	s.push(u);
	for (int i = 0; i < G[u].size(); i++) {
		int v = G[u][i];
		if (!dfn[v]) {
			tarjan(v);
			low[u] = min(low[u], low[v]);
		} else if (!color[v]) {
			low[u] = min(low[u], low[v]);
		}
	}
	if (dfn[u] == low[u]) {
		sum++;
		color[u] = sum;
		while (s.top() != u) {
			color[s.top()] = sum;
			s.pop();
		}
		s.pop();
	}
}

int main() {
	int x, y, z;
	cin >> n >> m;
	for (int i = 1; i <= m; ++i) {
		cin >> x >> y >> z;
		G[x].push_back(y);
		if (z == 2) G[y].push_back(x);
	}

	for (int i = 1; i <= n; ++i) {
		if (!dfn[i]) tarjan(i);
	}

	for (int i = 1; i <= n; ++i) {
		if (!col[color[i]]) sign[color[i]] = i;
		++col[color[i]]; 
	}

	col[0] = -1;
	int maxi = 0;
	for (int i = 1; i <= sum; ++i) {
		if (col[i] > col[maxi] || (col[i] == col[maxi] && sign[maxi] > sign[i])) {
			maxi = i;
		} 
	}

	printf("%d\n", col[maxi]);

	for (int i = sign[maxi]; i <= n; ++i) {
		if (color[i] == color[sign[maxi]]) printf("%d ", i);
	}
	printf("\n");
	return 0;
}